﻿Public Class fCCostos
    Dim daCCostos As lbGlobalcom.dsContabilidadTableAdapters.CON_CCOSTOTableAdapter
    Dim daEstCcost As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Dim daClave As lbGlobalcom.dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter

    Private Sub fCCostos_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Me.DsContabilidad.CON_CCOSTO.GetChanges IsNot Nothing Then
                CON_CCOSTOBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    CON_CCOSTOBindingNavigatorSaveItem.PerformClick()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
    Private Sub fCCostos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            daCCostos = New lbGlobalcom.dsContabilidadTableAdapters.CON_CCOSTOTableAdapter
            daClave = New lbGlobalcom.dsGlobalTableAdapters.GLO_CLAVESPRIMARIASTableAdapter
            daEstCcost = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
            daEstCcost.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "cc")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnCargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargar.Click
        Try
            If Me.DsContabilidad.CON_CCOSTO.GetChanges IsNot Nothing Then
                CON_CCOSTOBindingSource.EndEdit()
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    CON_CCOSTOBindingNavigatorSaveItem.PerformClick()
                End If
            End If
            daCCostos.Fill(DsContabilidad.CON_CCOSTO)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub CON_CCOSTOBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CON_CCOSTOBindingNavigatorSaveItem.Click
        Try
            Validate()
            CON_CCOSTOBindingSource.EndEdit()
            daCCostos.Update(DsContabilidad.CON_CCOSTO)
            MessageBox.Show("Se salvarón registros con exito", "Salva Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub CON_CCOSTODataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles CON_CCOSTODataGridView.CellBeginEdit
        Try
            If e.ColumnIndex = 1 AndAlso CON_CCOSTODataGridView.Rows(e.RowIndex).Cells(0).Value Is DBNull.Value Then
                Dim dt As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASDataTable
                dt = daClave.GetDataByIdClavePrimaria("CON_CCOSTO")
                If dt.Count Then
                    Dim dr As lbGlobalcom.dsGlobal.GLO_CLAVESPRIMARIASRow
                    dr = dt.Rows(0)
                    Dim Cons As lbGlobalcom.ConsecTablas = New lbGlobalcom.ConsecTablas(dr("TABLA"), dr("CAMPO"), dr("LONGITUD"))
                    CON_CCOSTODataGridView.Rows(e.RowIndex).Cells(0).Value = Cons.NumeroConsec
                    Cons = Nothing
                    CON_CCOSTOBindingSource.EndEdit()
                    daCCostos.Update(DsContabilidad.CON_CCOSTO)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub CON_CCOSTODataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles CON_CCOSTODataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub

    Private Sub CON_CCOSTODataGridView_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles CON_CCOSTODataGridView.UserDeletedRow
        Try
            daCCostos.Update(DsContabilidad.CON_CCOSTO)
            MessageBox.Show("Se elimino registro con exito", "Elimina Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try

    End Sub

    Private Sub CON_CCOSTODataGridView_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles CON_CCOSTODataGridView.UserDeletingRow
        If MessageBox.Show("Esta seguro que desea eliminar este registro", "Elimina Registro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End Sub


End Class